package com.zy.cat.controller.app.v1;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.entity.app.CatAppPvMenus;
import com.zy.cat.entity.app.resp.CatAppNoticeResp;
import com.zy.cat.entity.app.resp.CatEventElementResp;
import com.zy.cat.entity.dev.CatDevUserInfo;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatAppService;
import com.zy.cat.service.CatDevService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @info
 * @author Link
 * @date 2019-11-12 16:31
 */
@Api(value = "cat_app_info", tags = { "9趣猫APP信息统计开放API" }, description = "9趣猫APP信息统计开放接口")
@RestController
@RequestMapping("/v1/app")
@Slf4j
public class CatAPPController {

	@Autowired
	CacheService cacheService;
	
	//@Autowired
	CatDevService catDevService;

	@Autowired
	CatAppService catAppService;

	@PostMapping("/pv/info")
	@ApiOperation(position = 1, value = "统计APP,PV信息", notes = "统计APP,PV信息，根据每个页面的mid和子页面id统计每个页面的浏览量", response = RSPE.class, responseContainer = "Map", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "", example = "862052038380307", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "brand", value = "手机品牌", example = "Honer", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "model", value = "品牌型号", example = "HSF-001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imei", value = "手机IMEI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imsi", value = "手机IMSI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "mid", value = "主菜单ID", example = "101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cmid", value = "子菜单ID,为零统计主菜单的访问量", example = "0", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_uuid_rtype_imei_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_uuid_rtype_imei", MD5 = "token")
	public Object setAppPV(Integer rtype,@RequestParam(required = false,defaultValue = "") String pid,@RequestParam(required = false,defaultValue = "") String utoken, String brand, String model, String vscode, String cid, String imei,
			String imsi, String mid, String cmid, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if(StringUtils.isBlank(mid)) {
			return new RSPE<Object>(-3, "mid参数错误！");
		}
		if(StringUtils.isBlank(cmid)) {
			return new RSPE<Object>(-4, "cmid参数错误！");
		}
		//如果存在pid则判断是否存在此用户
		if (!StringUtils.isBlank(pid)) {
			CatDevUserInfo userInfo = catDevService.getCatDevUserInfoByPid(pid);
			if (userInfo==null) {
				return new RSPE<Object>(-5, "用户信息异常！");
			}	
		}
		catAppService.insertPV(rtype,pid,brand, model, vscode, cid, imei, imsi, mid, cmid);		
		return new RSPE<Object>(1, "success");
	}
	
	@PostMapping("/uv/info")
	@ApiOperation(position = 2, value = "统计APP,UV信息", notes = "包含，日活、留存等信息，每日仅请求一次，多次不做统计", response = RSPE.class, responseContainer = "Map", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户ID", example = "862052038380307", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "brand", value = "手机品牌", example = "Honer", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "model", value = "品牌型号", example = "HSF-001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imei", value = "手机IMEI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imsi", value = "手机IMSI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_uuid_rtype_imei_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_uuid_rtype_imei", MD5 = "token")
	public Object setAppUV(Integer rtype,@RequestParam(required = false,defaultValue = "") String pid,@RequestParam(required = false,defaultValue = "") String utoken, String brand, String model, String vscode, String cid, String imei,
			String imsi, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		//如果存在pid则判断是否存在此用户
		if (!StringUtils.isBlank(pid)) {
			CatDevUserInfo userInfo = catDevService.getCatDevUserInfoByPid(pid);
			if (userInfo==null) {
				return new RSPE<Object>(-5, "用户信息异常！");
			}
		}
		catAppService.insertUV(rtype,pid, brand, model, vscode, cid, imei, imsi );
		return new RSPE<Object>(1, "success");
	}
	
	@PostMapping("/pv/menus/info")
	@ApiOperation(position = 3, value = "获取APP,PV信息的菜单信息", notes = "", response = CatAppPvMenus.class, responseContainer = "List", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "brand", value = "手机品牌", example = "Honer", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "model", value = "品牌型号", example = "HSF-001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imei", value = "手机IMEI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imsi", value = "手机IMSI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "mid", value = "主菜单ID(0.全部)", example = "101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cmid", value = "子菜单ID(0.全部)", example = "101001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_uuid_mid_cmid_rtype_imei_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_uuid_mid_cmid_rtype_imei", MD5 = "token")
	public Object getAppPVMenus(Integer rtype, String brand, String model, String vscode, String cid, String imei,
			String imsi, String mid, String cmid, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<List<CatAppPvMenus>>> result=new Callable<RSPE<List<CatAppPvMenus>>>() {
			@Override
			public RSPE<List<CatAppPvMenus>> call() throws Exception {
				return new RSPE<List<CatAppPvMenus>>(catAppService.getAppPVMenus(rtype, brand, model, vscode, cid, imei, imsi, mid, cmid));
			}
		};
 
		return result;
	}
	
	@PostMapping("/message/info")
	@ApiOperation(position = 4, value = "用户获取推送的消息", notes = "个人中心获取推送消息", response = CatAppNoticeResp.class, responseContainer = "Map", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(pid_uuid_rtype_utoken_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	
	@CheckingMD5(checkingStr = "pid_uuid_rtype_utoken", MD5 = "token")
	@CheckDevPermission
	public Object setMessageInfo(Integer rtype,  String pid,String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CatAppNoticeResp>> result=new Callable<RSPE<CatAppNoticeResp>>() {
			@Override
			public RSPE<CatAppNoticeResp> call() throws Exception {
				CatAppNoticeResp atAppNotice=catAppService.getMessageInfo(rtype, pid);
				if (atAppNotice==null) {
					return new RSPE<CatAppNoticeResp>(0,"暂无消息");
				}
				return new RSPE<CatAppNoticeResp>(atAppNotice);
			}
		};
		return result;
	}
	
	@PostMapping("/event/count")
	@ApiOperation(value = "事件统计", notes = "统计用户APP内触发的事件", response = RSPE.class, responseContainer = "Map", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "appId", value = "应用分类(0.九趣猫、1.淘淘集)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "", example = "862052038380307", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = false, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "brand", value = "手机品牌", example = "Honer", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "model", value = "品牌型号", example = "HSF-001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imei", value = "手机IMEI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "imsi", value = "手机IMSI,IOS:IDFA", example = "862052038380307", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "eventData", value = "事件id,以逗号分隔", example = "101,100,20", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_uuid_rtype_imei_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_uuid_rtype_imei", MD5 = "token")
	public Object setEventCount(Integer rtype,Integer appId,@RequestParam(required = false,defaultValue = "") String pid,@RequestParam(required = false,defaultValue = "") String utoken, String brand, String model, String vscode, String cid, String imei,
			String imsi, String eventData, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (appId == null || (appId != 0 && appId != 1)) {
			return new RSPE<Object>(-3, "appId参数错误！");
		}
		if(StringUtils.isBlank(eventData)) {
			return new RSPE<Object>(-4, "事件数据不能为空！");
		}
		//如果存在pid则判断是否存在此用户
		if (!StringUtils.isBlank(pid)) {
			CatDevUserInfo userInfo = catDevService.getCatDevUserInfoByPid(pid);
			if (userInfo==null) {
				return new RSPE<Object>(-5, "用户信息异常！");
			}	
		}
		catAppService.setEventCount(rtype,appId,pid,brand, model, vscode, cid, imei, imsi, eventData);		
		return new RSPE<Object>(1, "success");
	}
	
	@PostMapping("/event/element")
	@ApiOperation(position = 3, value = "获取事件列表", notes = "", response = CatEventElementResp.class, responseContainer = "List", consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "vscode", value = "APP版本号", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "APP友盟渠道ID", example = "chan101", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(cid_uuid_vscode_rtype_imei_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "cid_uuid_vscode_rtype_imei", MD5 = "token")
	public Object getEventList(Integer rtype, String vscode, String cid, String imei,
			String imsi, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<List<CatEventElementResp>>> result=new Callable<RSPE<List<CatEventElementResp>>>() {
			@Override
			public RSPE<List<CatEventElementResp>> call() throws Exception {
				return new RSPE<List<CatEventElementResp>>(catAppService.getEventElementList(rtype, vscode, cid, imei, imsi));
			}
		};
		return result;
	}

}
